package com.demo2do.fms.admin.entity.search.business;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 * 外包指令相关查询
 * 
 * @author Chang Liu
 */
public class OutShipmentSearch implements QueryBuilder {
    
    /** 所选择公司 */
    private List<Long> organizationIds;
    
    /** 安吉调度指令号 */
    private String shipmentNumber;
    
    /** 驳运车号 */
    private Long transMeansId;
    
    /** 起始地 */
    private Long srcWhId;
    
    /** 目的地 */
    private Long destWhId;
    
    /** 承运方*/
    private Long transCorpId;
    
    /** 开始日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginShipmentDate;
    
    /** 结束日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endShipmentDate;
    
    /** 开始装运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginLoadingTime;
    
    /** 结束装运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endLoadingTime;
    
    /** 开始起运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date beginLeavingTime;
    
    /** 结束起运日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endLeavingTime;
    
    /** 调度指令状态 */
    private Long status;
        
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;

    /**
     * the default constructor
     */
    public OutShipmentSearch() {
        queryOrder = new QueryOrder("leavingTime", "DESC");
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(queryFilter == null || refresh) {

            queryFilter = new QueryFilter().addEquals("shipmentNumber", shipmentNumber)
                                           .addEquals("transMeansId", transMeansId)
                                           .addEquals("srcWhId", srcWhId)
                                           .addEquals("destWhId", destWhId)
                                           .addEquals("transCorpId", transCorpId)
                                           .addDateTimeBetween("createTime", beginShipmentDate, endShipmentDate)
                                           .addDateTimeBetween("loadingTime", beginLoadingTime, endLoadingTime)
                                           .addDateTimeBetween("leavingTime", beginLeavingTime, endLeavingTime)
                                           .addEquals("status", status)
                                           .addSentence("balanceId IN (:organizationIds)", "organizationIds", organizationIds);
                                           
            
        }
        
        return queryFilter;
    }
    
    /**
     * 
     * @param transCorpIds
     */
    public void addTransCorpId(List<Long> transCorpIds) {
        
        // add transCorpIds into the sentence
        this.getPreparedFilter(false)
            .addSentence("transCorpId IN (:outSystemOrgIds)", "outSystemOrgIds", transCorpIds);
        
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM ShipmentLine WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + queryOrder.sentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * 
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }

    /**
     * @return the organizationIds
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the shipmentNumber
     */
    public String getShipmentNumber() {
        return shipmentNumber;
    }

    /**
     * @return the transMeansId
     */
    public Long getTransMeansId() {
        return transMeansId;
    }

    /**
     * @return the srcWhId
     */
    public Long getSrcWhId() {
        return srcWhId;
    }

    /**
     * @return the destWhId
     */
    public Long getDestWhId() {
        return destWhId;
    }

    /**
     * @return the transCorpId
     */
    public Long getTransCorpId() {
        return transCorpId;
    }

    /**
     * @return the beginShipmentDate
     */
    public Date getBeginShipmentDate() {
        return beginShipmentDate;
    }

    /**
     * @return the endShipmentDate
     */
    public Date getEndShipmentDate() {
        return endShipmentDate;
    }

    /**
     * @return the beginLoadingTime
     */
    public Date getBeginLoadingTime() {
        return beginLoadingTime;
    }

    /**
     * @return the endLoadingTime
     */
    public Date getEndLoadingTime() {
        return endLoadingTime;
    }

    /**
     * @return the beginLeavingTime
     */
    public Date getBeginLeavingTime() {
        return beginLeavingTime;
    }

    /**
     * @return the endLeavingTime
     */
    public Date getEndLeavingTime() {
        return endLeavingTime;
    }

    /**
     * @return the status
     */
    public Long getStatus() {
        return status;
    }

    /**
     * @return the queryOrder
     */
    public QueryOrder getQueryOrder() {
        return queryOrder;
    }

    /**
     * @return the queryFilter
     */
    public QueryFilter getQueryFilter() {
        return queryFilter;
    }

    /**
     * @param organizationIds the organizationIds to set
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param shipmentNumber the shipmentNumber to set
     */
    public void setShipmentNumber(String shipmentNumber) {
        this.shipmentNumber = StringUtils.trim(shipmentNumber);
    }

    /**
     * @param transMeansId the transMeansId to set
     */
    public void setTransMeansId(Long transMeansId) {
        this.transMeansId = transMeansId;
    }

    /**
     * @param srcWhId the srcWhId to set
     */
    public void setSrcWhId(Long srcWhId) {
        this.srcWhId = srcWhId;
    }

    /**
     * @param destWhId the destWhId to set
     */
    public void setDestWhId(Long destWhId) {
        this.destWhId = destWhId;
    }

    /**
     * @param transCorpId the transCorpId to set
     */
    public void setTransCorpId(Long transCorpId) {
        this.transCorpId = transCorpId;
    }

    /**
     * @param beginShipmentDate the beginShipmentDate to set
     */
    public void setBeginShipmentDate(Date beginShipmentDate) {
        this.beginShipmentDate = beginShipmentDate;
    }

    /**
     * @param endShipmentDate the endShipmentDate to set
     */
    public void setEndShipmentDate(Date endShipmentDate) {
        this.endShipmentDate = endShipmentDate;
    }

    /**
     * @param beginLoadingTime the beginLoadingTime to set
     */
    public void setBeginLoadingTime(Date beginLoadingTime) {
        this.beginLoadingTime = beginLoadingTime;
    }

    /**
     * @param endLoadingTime the endLoadingTime to set
     */
    public void setEndLoadingTime(Date endLoadingTime) {
        this.endLoadingTime = endLoadingTime;
    }

    /**
     * @param beginLeavingTime the beginLeavingTime to set
     */
    public void setBeginLeavingTime(Date beginLeavingTime) {
        this.beginLeavingTime = beginLeavingTime;
    }

    /**
     * @param endLeavingTime the endLeavingTime to set
     */
    public void setEndLeavingTime(Date endLeavingTime) {
        this.endLeavingTime = endLeavingTime;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(Long status) {
        this.status = status;
    }

    /**
     * @param queryOrder the queryOrder to set
     */
    public void setQueryOrder(QueryOrder queryOrder) {
        this.queryOrder = queryOrder;
    }

    /**
     * @param queryFilter the queryFilter to set
     */
    public void setQueryFilter(QueryFilter queryFilter) {
        this.queryFilter = queryFilter;
    }
    
}
